package kr.saeildamil.androidStudy_second.gesture;

import java.util.ArrayList;

import kr.saeildamil.androidStudy_second.R;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomGesture extends Activity {
	TextView mTextCount;
	int mCount = 0;
	GestureLibrary mGesLib;
	ImageView iv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch31_customgesture);

		mTextCount = (TextView) findViewById(R.id.count);
		mTextCount.setText(Integer.toString(mCount));
		iv = (ImageView) findViewById(R.id.moon1);

		mGesLib = GestureLibraries.fromRawResource(this, R.raw.hangul);
		if (mGesLib.load() == false) {
			finish();
		}

		GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
		gestures.addOnGesturePerformedListener(mListener);
	}

	OnGesturePerformedListener mListener = new OnGesturePerformedListener() {
		public void onGesturePerformed(GestureOverlayView overlay,
				Gesture gesture) {
			ArrayList<Prediction> predictions = mGesLib.recognize(gesture);

			if (predictions.size() != 0) {
				Prediction prediction = predictions.get(0);
				String name = prediction.name;
				if (prediction.score > 1.0) {
					if (name.equals("kiyuk")) {
						mCount++;
					} else if (name.equals("niun")) {
						mCount--;
					} else if (name.equals("digut")) {
						mCount= 0;
					}
					if(mCount== 5 || mCount== 8){
						iv.setVisibility(View.VISIBLE);
					} else {
						iv.setVisibility(View.INVISIBLE);
					}
					mTextCount.setText("" + mCount);
				}

			}
		}
	};

}
